<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#">
  <!ENTITY dc "http://purl.org/dc/elements/1.1/">
  <!ENTITY dcterms "http://purl.org/dc/terms/">
  <!ENTITY foaf "http://xmlns.com/foaf/0.1/">
  <!ENTITY a "http://xmlns.heustics.org/schema/myelin/0.1/">
  <!ENTITY m "http://xmlns.heustics.org/ontology/myelin/0.1/">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:a="&a;"
  xmlns:l="http://xmlns.heustics.org/schema/i18n/0.1/"
  xmlns:o="&a;NodeAcl#view"  
  xmlns:b="&a;Document#view"
  exclude-result-prefixes="xsl a b l o"
>
  <xsl:variable name="o:l10n" select="a:get-l10n('&a;NodeAcl#view')"/>
  <xsl:variable name="o:actions-l10n" select="$o:l10n/l:context[@name='actions']/*"/>
  <xsl:variable name="o:new-actions-l10n" select="$o:l10n/l:context[@name='new-actions']/*"/>  
  <xsl:variable name="o:acls-l10n" select="$o:l10n/l:context[@name='acls']/*"/>
  <xsl:variable name="o:titles-l10n" select="$o:l10n/l:context[@name='titles']/*"/>
  
  <xsl:variable name="o:can-delete-acl-group" select="a:check-acl('delete-acl-group')"/>
  <xsl:variable name="o:can-edit-acl-group" select="a:check-acl('edit-acl-group')"/>
    
  <xsl:template match="a:title[@key='view-acls']" mode="a:head-content">
    <title>
      <xsl:value-of select="concat($o:titles-l10n[@key='view-acls'],.)"/>
    </title>        
  </xsl:template>
  
  <xsl:template match="a:title[@key='view-acls']" mode="a:page-header">
    <xsl:call-template name="a:page-header-title">
      <xsl:with-param name="title" select="concat($o:titles-l10n[@key='view-acls'],.)"/>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="a:acl" mode="a:node-action" name="o:node-action">
    <xsl:param name="key" select="string(@key)"/>
    <xsl:param name="label" select="$o:actions-l10n[@key=$key]"/>
    <xsl:if test="$label">
      <xsl:call-template name="a:node-action">
        <xsl:with-param name="key" select="$key"/>
        <xsl:with-param name="label" select="$label"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template> 
  
  <xsl:template match="a:acl" mode="a:select-action">
    <xsl:param name="key" select="string(@key)"/>
    <xsl:param name="label" select="$o:new-actions-l10n[@key=$key]"/>
    <xsl:if test="$label">
      <xsl:call-template name="a:select-action">
        <xsl:with-param name="key" select="$key"/>
        <xsl:with-param name="label" select="$label"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template> 
  
  <xsl:template match='a:grouping[@key="acls"]'>
    <div class="body acls">
      <span><xsl:value-of select="$o:l10n/l:gentext[@key='intro']"/></span>
      <div class="spacer"/>
    </div>
    <xsl:choose>
      <xsl:when test="$o:can-delete-acl-group">
        <xsl:variable name="form-l10n" select="$o:l10n/l:context[@name='container-form-buttons']/*"/>
        <div class="container-form-wrapper">
          <form name="acl-container-form" class="container-form acls-container-form"
            action="{a:format-node-action('delete-acl-group')}" method="post">
            <input type='hidden' name='view-acls' value='1'/>
            <xsl:call-template name="a:container"/>  
            <xsl:if test="number(@total) &gt; 0">
              <div class='submits'>  
                <div class="input submit select-all">
                  <input name="select-all" type="button" value="{$form-l10n[@key='select-all']}"
                    onClick="select-all(this,'acl-container-form','acl-group-tag')"/>
                  <div class="spacer"/>
                </div>
                <div class="input submit reset">
                  <xsl:variable name='val' select="$form-l10n[@key='reset']"/>
                  <input type='reset' name='reset' value='{$val}'/>
                  <div class='spacer'/>
                </div>      
                <div class="input submit delete">
                  <xsl:variable name='val' select="$form-l10n[@key='delete']"/>
                  <input type='hidden' name='init' value='{$val}'/>
                  <input type='submit' name='event' value='{$val}'/>
                  <div class='spacer'/>
                </div>      
                <div class="spacer"/>
              </div>  
            </xsl:if>
            <div class="spacer"/>
          </form>
          <div class="spacer"/>
        </div>       
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="a:container"/>
      </xsl:otherwise>
    </xsl:choose>   
   </xsl:template>  
  
  <xsl:template match="a:grouping[@key='acls']" mode="a:container-header">
    <div class="label acls">
      <span>
        <xsl:value-of select="$o:l10n/l:gentext[@key='acls-label']"/>          
      </span>   
      <div class="spacer"/> 
    </div>    
  </xsl:template>
  
  <xsl:template match="a:acl-group" mode="a:container-node"> 
    <xsl:param name="theme" select="$b:theme/l:context[@name='container-node']/*"/>
    <div class="grouping acl-group">
      <div class="key grouping-key acl-group-key">
        <div class="label">
          <xsl:value-of select="$o:acls-l10n[@key='acl-group']"/>
        </div>
        <div class="tag acl-group-tag">
          <xsl:if test="$o:can-delete-acl-group">
            <input type="checkbox" name="acl-group-tag" value="{@tag}"/>    
          </xsl:if>
          <xsl:if test="$o:can-edit-acl-group">
            <a class="acl-group-action edit-img" href="{a:format-url-ext(
              a:format-node-action('edit-acl-group'),
              'acl-group-tag',@tag)}"
              title="{$o:l10n/*[@key='edit-acl-group']}">
              <xsl:variable name="edit-img" select="$theme[@key='edit']"/>
              <img class="container-node-action"
                src="{a:format-static-url(a:public-static-url($edit-img/@uri))}"
                alt="{$o:l10n/*[@key='edit-acl-group']}"/>
            </a>              
          </xsl:if>         
          <span>
            <xsl:value-of select="@tag"/>
          </span>
          <div class="spacer"/>
        </div>
        <div class="spacer"/>
      </div>
      <div class="value grouping-value acl-group-value">
        <xsl:apply-templates mode="a:acl-container-node"/>
        <div class="spacer"/>
      </div>      
      <div class="spacer"/>
    </div>    
  </xsl:template>
  
  <xsl:template match="a:actions" mode="a:acl-container-node">
    <div class="entry actions">
      <div class="key entry-key actions-key">
        <div class="label">
          <span>
            <xsl:value-of select="$o:acls-l10n[@key='actions']"/>
          </span>
          <div class="spacer"/>
        </div>       
        <div class="spacer"/>
      </div>
      <div class="value entry-value actions-value">
        <xsl:choose>
          <xsl:when test="*">
            <ul>        
              <xsl:apply-templates mode="a:app-list">
                <xsl:sort select="@key"/>
              </xsl:apply-templates>           
            </ul>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$o:acls-l10n[@key='no-actions']"/>
          </xsl:otherwise>
        </xsl:choose>        
        <div class="spacer"/>
      </div>
    </div>
  </xsl:template>
  
  <xsl:template match="a:app" mode="a:app-list">
    <li class="section app">
      <div class="key section-key app-key">
        <div class="label">
          <span>
            <xsl:value-of select="$o:acls-l10n[@key='app-key']"/>
          </span>
          <div class="spacer"/>
        </div>
        <div class="tag">
          <span>
            <xsl:value-of select="@key"/>
          </span>
          <div class="spacer"/>
        </div>        
        <div class="spacer"/>
      </div>
      <div class="value section-value app-value">
        <ul>
          <xsl:apply-templates mode="a:action-type-list">
            <xsl:sort select="@key"/>            
          </xsl:apply-templates>
        </ul>                
        <div class="spacer"/>
      </div>
      <div class="spacer"/>
    </li>            
  </xsl:template>
  
  <xsl:template match="a:action-type" mode="a:action-type-list">
    <li class="action">
      <span>
        <xsl:value-of select="@key"/>
      </span>
    </li>
  </xsl:template>
  
  <xsl:template match="a:permissions" mode="a:acl-container-node">
    <div class="entry permissions">
      <div class="key entry-key permissions-key">
        <div class="label">
          <span>
            <xsl:value-of select="$o:acls-l10n[@key='permissions']"/>
          </span>
          <div class="spacer"/>
        </div>        
        <div class="spacer"/>
      </div>
      <div class="value entry-value permissions-value">
        <xsl:apply-templates mode="a:permissions-list"/>    
        <div class="spacer"/>    
      </div>
    </div>
  </xsl:template> 
  
  <xsl:template match="a:allow|a:forbid" mode="a:permissions-list">
    <xsl:param name="key" select="local-name()"/>
    <div class="section permissions-list {$key}">
      <div class="key section-key permissions-list-key">
        <div class="label">
          <span>
            <xsl:value-of select="$o:acls-l10n[@key=$key]"/>
          </span>
          <div class="spacer"/>
        </div>
        <div class="spacer"/>
      </div>
      <div class="value section-value permissions-list-value">
        <ul>
          <xsl:choose>
            <xsl:when test="*">
              <xsl:apply-templates mode="a:permissions-agent">
                <xsl:sort select="@nick"/>
              </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
              <li class="agent nobody">
                <span>
                  <xsl:value-of select="$o:acls-l10n[@key='nobody']"/>
                </span>
                <div class="spacer"/>
              </li>
            </xsl:otherwise>
          </xsl:choose>          
        </ul>                
        <div class="spacer"/>
      </div>
      <div class="spacer"/>
    </div>
  </xsl:template> 
  
  <xsl:template match="a:world" mode="a:permissions-list">
    <div class="section permissions-list allow-world">
      <div class="key section-key permissions-list-key allow-world-key">
        <div class="label">
          <span>
            <xsl:value-of select="$o:acls-l10n[@key='world']"/>
          </span>
          <div class="spacer"/>
        </div>
        <div class="spacer"/>
      </div>
      <div class="value section-value permissions-list-value allow-world-value">
        <span>
          <xsl:variable name="value" select="string(.)"/>
          <xsl:choose>
            <xsl:when test="$value='1'">
              <xsl:value-of select="$o:acls-l10n[@key='allowed']"/>
            </xsl:when>
            <xsl:when test="$value='0'">
              <xsl:value-of select="$o:acls-l10n[@key='forbidden']"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$o:acls-l10n[@key='unset']"/>
            </xsl:otherwise>
          </xsl:choose>
        </span>
        <div class="spacer"/>
      </div>
      <div class="spacer"/>
    </div>
  </xsl:template> 
  
  <xsl:template match="a:agent" mode="a:permissions-agent">
    <li class="agent">
      <span>
        <xsl:value-of select="@nick"/>
      </span>  
      <div class="spacer"/>
    </li>
  </xsl:template>
 
  <xsl:template match="a:agent[@profile-id]" mode="a:permissions-agent">
    <li class="agent">
      <a href="{a:format-url(a:public-url(@profile-id))}" 
        title="{$o:l10n/l:gentext[@key='view-profile']}">
        <xsl:choose>
          <xsl:when test="@nick">
            <xsl:value-of select='@nick'/>
          </xsl:when>
          <xsl:when test="@name">
            <xsl:value-of select='@name'/>
          </xsl:when>
        </xsl:choose>            
      </a>          
      <div class="spacer"/>         
    </li>
  </xsl:template>  
  
</xsl:stylesheet>